package com.ruoyi.controller;

import com.ruoyi.domain.MFile;
import com.ruoyi.dto.RepFile;
import com.ruoyi.platform.annotation.Log;
import com.ruoyi.platform.core.controller.BaseController;
import com.ruoyi.platform.core.domain.AjaxResult;
import com.ruoyi.platform.core.domain.R;
import com.ruoyi.platform.core.page.TableDataInfo;
import com.ruoyi.platform.enums.BusinessType;
import com.ruoyi.platform.utils.poi.ExcelUtil;
import com.ruoyi.service.IMFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 文件存储表Controller
 *
 * @author ruoyi
 * @date 2022-09-26
 */
@RestController
@RequestMapping("/admin/file")
@Api(tags = {"文件存储表服务"})
public class FileController extends BaseController {

    @Autowired
    private IMFileService mFileService;

    /**
     * 查询文件存储表列表
     */
    @PreAuthorize("@ss.hasPermi('admin:MFile:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询文件存储表列表", httpMethod = "GET", notes = "查询文件存储表列表")

    public TableDataInfo list(MFile mFile) {
        startPage();
        List<MFile> list = mFileService.selectMFileList(mFile);
        return getDataTable(list);
    }

    /**
     * 导出文件存储表列表
     */
    @PreAuthorize("@ss.hasPermi('admin:MFile:export')")
    @Log(title = "文件存储表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出文件存储表列表", httpMethod = "POST", notes = "导出文件存储表列表")
    public void export(HttpServletResponse response, MFile mFile) {
        List<MFile> list = mFileService.selectMFileList(mFile);
        ExcelUtil<MFile> util = new ExcelUtil<>(MFile.class);
        util.exportExcel(response, list, "文件存储表数据");
    }

    /**
     * 获取文件存储表详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:MFile:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取文件存储表详细信息", httpMethod = "GET", notes = "获取文件存储表详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(mFileService.selectMFileById(id));
    }

    /**
     * 新增文件存储表
     */
    @PreAuthorize("@ss.hasPermi('admin:MFile:add')")
    @Log(title = "文件存储表", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增文件存储表", httpMethod = "POST", notes = "新增文件存储表")
    public AjaxResult add(@RequestBody MFile mFile) {
        return toAjax(mFileService.insertMFile(mFile));
    }

    /**
     * 修改文件存储表
     */
    @PreAuthorize("@ss.hasPermi('admin:MFile:edit')")
    @Log(title = "文件存储表", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改文件存储表", httpMethod = "PUT", notes = "修改文件存储表")
    public AjaxResult edit(@RequestBody MFile mFile) {
        return toAjax(mFileService.updateMFile(mFile));
    }

    /**
     * 删除文件服务
     */
    @PreAuthorize("@ss.hasPermi('admin:goodsFile:remove')")
    @Log(title = "文件服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除文件服务", httpMethod = "DELETE", notes = "删除文件服务")
    public AjaxResult remove(@ApiParam(name = "id", value = "文件ID", required = true)
                             @PathVariable Long id) {
        return toAjax(mFileService.removeFileById(id));
    }

    /**
     * 删除文件存储表
     */
    @PreAuthorize("@ss.hasPermi('admin:MFile:remove')")
    @Log(title = "文件存储表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除文件存储表", httpMethod = "DELETE", notes = "删除文件存储表")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mFileService.deleteMFileByIds(ids));
    }

    /**
     * 上传文件到服务器,,返回一个文件url和文件id
     */
    @PostMapping("/uploadFile")
    @Log(title = "上传文件到服务器,,返回一个文件url和文件id", businessType = BusinessType.OTHER)
    @ApiOperation(value = "上传文件", httpMethod = "POST", notes = "上传文件")
    public R<RepFile> uploadFile(@ApiParam(name = "file", value = "上传文件对象", required = true)
                                 @RequestParam(value = "file") MultipartFile file,
                                 @ApiParam(name = "type", value = "文件类型", required = true)
                                 @RequestParam(value = "type") String type) throws Exception {
        return R.ok(mFileService.uploadFile(file, type));
    }

    /**
     * 批量上传文件
     */
    @PostMapping("/uploadFileBatch")
    @Log(title = "批量上传文件", businessType = BusinessType.OTHER)
    @ApiOperation(value = "批量上传文件", httpMethod = "POST", notes = "批量上传文件")
    public Map<String,String> uploadFileBatch(@ApiParam(name = "files", value = "批量上传文件对象", required = true)
                                                  @RequestParam(value = "files") MultipartFile[] files)  {


        return null;
    }

    /**
     * 下载文件
     *
     * @param res
     * @param req
     * @param id  文件ID
     */
    @GetMapping("/downloadFile")
    @ApiOperation(value = "下载文件", httpMethod = "GET", notes = "下载文件")
    @Log(title = "下载文件", businessType = BusinessType.OTHER)
    public void downloadFile(HttpServletResponse res, HttpServletRequest req,
                             @ApiParam(name = "id", value = "文件ID", required = true) @RequestParam Long id) {
        mFileService.downloadFile(res, req, id);
    }

    @PostMapping("/downloadFileByDocId")
    @ApiOperation(value = "下载文件", httpMethod = "GET", notes = "下载文件")
    public void downloadFileByDocID(HttpServletRequest req,
                                    HttpServletResponse res,
                                    @ApiParam(name = "documentId", value = "文件ID", required = true) @RequestParam(value = "documentId") Long documentId) {
        mFileService.downloadFileByDocId(res, req, documentId);
    }

}
